﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Dapper;
using LibMS.Models;

namespace LibMS.Controllers
{
    [Authorize(Roles = "System Admin, Lib Admin")]
    public class ApiLibUserController : ApiController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetData(int index, int size)
        {
            int? total;
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@PageSize", size);
            parameters.Add("@PageIndex", index);
            var data = _unitOfWork.CmsUser.GetData(out total, out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var sqlResult = data as List<CmsUser> ?? null;
            var result = new
            {
                success = true,
                message = string.Empty,
                total = total,
                data = sqlResult
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetById(int userId)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@UserId", userId);
            var data = _unitOfWork.CmsUser.GetById(out message, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var result = new
            {
                success = true,
                message = string.Empty,
                data = data
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [AllowAnonymous]
        [System.Web.Http.HttpGet]
        public HttpResponseMessage GetByUsername(string username)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@UserName", username);
            var data = _unitOfWork.CmsUser.QuerySpNT(out message, StringConstant.TABLE_CMSUSER + StringConstant.SP_CMSUSER_GETUSERBYUSERNAME, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var sqlResult = data as List<CmsUser> ?? null;
            var result = new
            {
                success = true,
                message = string.Empty,
                data = sqlResult
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }

        [Authorize(Roles = "System Admin")]
        [System.Web.Http.HttpPost]
        public HttpResponseMessage UpdateRoleForUser(LibUserRole entity)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@RoleId", entity.RoleId);
            parameters.Add("@UserId", entity.UserId);
            var data = _unitOfWork.LibUserRole.ExecuteSp(out message, StringConstant.TABLE_LIBUSERROLE + StringConstant.SP_LIBUSERROLE_UPDATEROLEFORUSER, parameters);
            if (data == null)//Query error
            {
                var error = new
                {
                    success = false,
                    message = message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
            //Success
            var result = new
            {
                success = true,
                message = string.Empty,
                data = entity
            };
            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }
}
